var Tree = function() {
	//操作树变量
	this.zTree = null;
	this.URL_TOPO = _ctxStatic + '/images/topo.png';
	this.URL_STATION = _ctxStatic + '/images/station.png';
	this.URL_NE = _ctxStatic + '/images/finder.png';
	this.URL_CARD = _ctxStatic + '/images/card.png';
	this.URL_UNPLUG_CARD = _ctxStatic + '/images/unplug_card.png';
	this.URL_SLOT = _ctxStatic + '/images/slot.png';
	this.HIGH_LIGHT_COLOR = '#FF0000';
	this.TYPE_TOPO = "topo";
	this.TYPE_STATION = "station";
	this.TYPE_NE = "ne";
	this.TYPE_SLOT = "slot";
	this.TYPE_CARD = "card";
	this.CTX_NE_URL = _ctx + '/cfg/ne/';
	//针对标签 <wiget:module>
	this.INITIAL_MAP = {};
	this.lastValue = "";
	this.nodeList = [];
	this.fontCss = {};
	this.that;
	
	this.setting = {
		data : {
			key : {
				title : "t"
			},
			simpleData : {
				enable : true
			}
		},
		view : {
			fontCss : {}
		},
		callback : {
			onDblClick : {}
		}
	};

	/**
	 * 格式化图标
	 */
	this.formatIcons = function(treeData) {
		var that = this;
		$(treeData).each(function(index, data){
			switch(data.type) {
				case that.TYPE_TOPO:					
					data.icon = that.URL_TOPO; 
				break;
				case that.TYPE_STATION:					
					data.icon = that.URL_STATION; 
					break;
				case that.TYPE_SLOT:
					if(data.enable) {
						data.icon = that.URL_CARD;
					} else {
						data.icon = that.URL_SLOT;
					}
					break;	
				case that.TYPE_NE:
					data.icon = that.URL_NE;
					break;
				case that.TYPE_CARD:		
					data.icon = that.URL_CARD;
					break;
			}
		});
	};
	
	/**
	 * 初始化树
	 */
	this.initTree = function(treeId, treeData) {
		//转换图标
		this.formatIcons(treeData);
		
		//针对初始化无法赋值的情况
		this.setting.callback.onDblClick = this.onTreeItemDblClick;
		this.setting.callback.onClick = this.onTreeItemClick;
		this.setting.callback.onExpand = this.onExpand; 
		this.setting.view.fontCss = this.getFontCss;		
		
		$.fn.zTree.init($("#" + treeId), this.setting, treeData);
		this.zTree = $.fn.zTree.getZTreeObj(treeId);
		this.zTree.expandAll(false);
		that = this;
		that.zTree = this.zTree; 
	};
	
	/**
	 * 获取当前选择的Item
	 */
	this.getLstSelectedItem = function() {
		return this.selectedNode;
	};
	
	/**
	 * 展开节点
	 */
	this.onExpand = function(event, treeId, treeNode) {
		parent.commonAPI.setLstItem(treeNode);
		if(treeNode.type == that.TYPE_NE) {	
			that.updateAvailbleSlot(treeNode);
		}
	};

	/**
	 * 单击树，更新状态栏
	 */
	this.onTreeItemClick = function(event, treeId, treeNode, clickFlag){		
		parent.commonAPI.setLstItem(treeNode);
		$('#chosenMsg').html(treeNode.name);		
		$('#selectedPIDS').val(treeNode.pIds);
		$('#selectedPID').val(treeNode.id);		
		$('#selectedName').val(treeNode.name);		
		parent.commonAPI.setLstItem(treeNode);
		if(treeNode.type == that.TYPE_NE) {	
			that.updateAvailbleSlot(treeNode);
		}
	};

	/**
	 * 更新槽位是否可用
	 */
	this.updateAvailbleSlot = function(treeNode){
		var neId = treeNode.id.substr(treeNode.id.indexOf('_')+1);		
		var slotList = {};
		var childrenList = treeNode.children;
		$.get(that.CTX_NE_URL + 'queryNeCards/' + neId, function(result){		
			$(result).each(function(index,data){
				slotList[data.slot] = data;			
			});
			$(childrenList).each(function(index,data){
				//var cSlotId = parent.commonAPI.getTreeSlotId(data.id);
				var cSlotId = data.port;
				if(slotList[cSlotId] == null) {
					data.icon = that.URL_UNPLUG_CARD;
					data.enable = false;
				} else {
					data.icon = that.URL_CARD;
					data.enable = true;
				}
			});
			that.zTree.refresh();
		});
	};
	
	/**
	 * 点击树节点事件
	 * @param event
	 * @param treeId
	 * @param treeNode
	 * @param clickFlag
	 */
	this.onTreeItemDblClick = function(event, treeId, treeNode, clickFlag){
		parent.commonAPI.setLstItem(treeNode);
		//打开网元节点
		if(treeNode.type == that.TYPE_NE) {						
			parent.commonAPI.createTab(treeNode.id, treeNode.name, '/webbase/core/index/neChassis?nid=' + treeNode.id);			
		//如果是根节点，则不创建tab，只创建网元，板卡的tab		
		} else if(treeNode.type=='topo') {
			parent.commonAPI.selectTab('tabs-topo');
		//打开板卡
		} else if(treeNode.type == that.TYPE_SLOT) {
			if(treeNode.enable) {
				parent.commonAPI.createTab(treeNode.id, treeNode.name, '/webbase/core/index/cardConfig/' + treeNode.id);
			} else {
				parent.commonAPI.warn('提示','板卡未插入，请检测物理板卡是否插入槽位！');	
			}
		} else if(treeNode.type == that.TYPE_STATION) {
			parent.commonAPI.createTab(treeNode.id, treeNode.name, '/webbase/topo/subTopo/'+treeNode.id);
		}
	};
	
	/**
	 * 搜索树节点
	 * 
	 * @param value
	 */
	this.searchNode = function(value) {
		var keyType = "";
		keyType = "id";
		value = parseInt(value);
		if (that.lastValue === value)
			return;
		that.lastValue = value;
		if (value === "")
			return;
		that.updateNodes(false);
		
		that.zTree = $.fn.zTree.getZTreeObj("treeView");
		var node = that.zTree.getNodeByParam(keyType, value);
		if (node === null) {
			that.nodeList = [];
		} else {
			that.nodeList = [ node ];
		}
		
		that.updateNodes(true);
	};
	
	this.updateNodes = function(highlight) {
		that.zTree = $.fn.zTree.getZTreeObj("treeView");
		for ( var i = 0, l = that.nodeList.length; i < l; i++) {
			that.nodeList[i].highlight = highlight;
			that.zTree.updateNode(that.nodeList[i]);
		}
	};
	
	this.getFontCss = function(treeId, treeNode) {
		return (!!treeNode.highlight) ? {
			color : that.HIGH_LIGHT_COLOR,
			"font-weight" : "bold"
		} : {
			color : "#333",
			"font-weight" : "normal"
		};
	};

	this.filter = function(node) {
		return !node.isParent && node.isFirstNode;
	};
	return this;
}